package com.lsw.jgyf.ui.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lsw.jgyf.R;
import com.lsw.jgyf.ui.base.activity.BaseActivity;
import com.lsw.ninegridimageview.Image;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/8.
 */

public class PhoneLoginActivity extends BaseActivity<PhoneLoginPresenter> implements View.OnClickListener, PhoneLoginContract.View {
    @BindView(R.id.tv_fasong)
    TextView tv_fasong;
    @BindView(R.id.iv_mima_login)
    ImageView iv_mima_login;
    @BindView(R.id.tv_register)
    TextView tv_register;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_pass)
    EditText et_pass;
    @BindView(R.id.tv_login)
    TextView tv_login;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_phone;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        tv_fasong.setOnClickListener(this);
        iv_mima_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_fasong:
                mPresenter.sendCode(et_phone.getText().toString().trim(), "1");
                break;
            case R.id.iv_mima_login:
                finish();
                break;
            case R.id.tv_register:
                Intent intent2 = new Intent(mContext, RegisterActivity.class);
                startActivity(intent2);
                break;
            case R.id.tv_login:
                mPresenter.Login(et_phone.getText().toString().trim(), et_pass.getText().toString().trim());
                break;
        }
    }

    @Override
    public void setData() {

    }

    @Override
    public void sendSucess() {
        startCountDown(60000);
    }

    @Override
    public void loginSucceed() {
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private CountDownTimer countDownTimer;

    /**
     * 倒计时
     *
     * @param time
     */
    private void startCountDown(long time) {
        tv_fasong.setEnabled(false);
//        tv_fasong.setTextColor(mContext.getResources().getColor(R.color.gray));
        countDownTimer = new CountDownTimer(time, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_fasong.setText((millisUntilFinished / 1000) + "s后重新发送");
            }

            @Override
            public void onFinish() {
//                tv_fasong.setText(mContext.getResources().getString(R.string.verify_code));
//                tv_fasong.setBackground(mContext.getResources().getDrawable(R.drawable.round_blue_r));
                tv_fasong.setTextColor(mContext.getResources().getColor(R.color.color_86B74E));
                tv_fasong.setText("重新发送");
                tv_fasong.setEnabled(true);
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null)
            countDownTimer.cancel();
    }
}
